﻿using System;
using System.Collections;
using System.Collections.Generic;
using CIIS.Common;
using Contract.IService;
using Framework;
using System.Globalization;
using Contract.Domain;
using System.Data;
using System.Reflection;
using System.IO;
using MySql.Data.MySqlClient;

namespace CIIS
{
    public class Utils
    {
        public static readonly Utils Instance = new Utils();
        Utils()
        {
        }

        public IList GetDataList<T>(IDaoService dao)
        {
            return GetDataList(dao, typeof(T).Name);
        }

        public IList GetDataList(IDaoService dao, string tableName)
        {
            QueryInfo info = new QueryInfo();
            info.QueryObject = tableName;
            IList list = dao.FindList(info);
            return list;
        }


        public DropdownControlParam GetDcpFromEnums<T>()
        {
            return GetDcpFromEnums(typeof(T));
        }
        public DropdownControlParam GetDcpFromEnums(Type enumType)
        {
            string[] displays = Enum.GetNames(enumType);
            Array values = Enum.GetValues(enumType);

            List<object> dataList = new List<object>();
            for (int i = 0; i < displays.Length; i++)
            {
                DropDownListItem a = new DropDownListItem() { Name = displays[i], Value = ((int)values.GetValue(i)).ToString() };
                dataList.Add(a);
            }
            DropdownControlParam dcp = new DropdownControlParam() { DisplayMember = "Name", ValueMember = "Value", DataSource = dataList };
            return dcp;
        }

        /// <summary>
        /// 获取代号，分特情人员，刑嫌人员，阵控点等等
        /// </summary>
        /// <returns></returns>
        public string GetCode(string typeName,string col)
        {
            var u = DataUtils.Instance.SpringCache.Get("CurrentUser") as t_sys_user;
            string bh = "321";
            if (!string.IsNullOrEmpty(u.OfficeCode))
            {
                if (u.OfficeCode.Length >= 8)
                {
                    bh += u.OfficeCode.Substring(0, 8);
                }
                else
                {
                    bh += u.OfficeCode;
                    for (var i = 0; i < 8-u.OfficeCode.Length; i++)
                    {
                        bh += "0";
                    }
                }
            }
            else
            {
                bh += "00000000";
            }
            bh += DateTime.Now.Year.ToString();
            string sql = string.Format("select max(substring({0},1,21)) from {1} ", col, typeName);
            Framework.QueryInfo info=new QueryInfo();
            info.CustomSQL=sql;
            info.Where.Add("kk", string.Format(" and {0} like '{1}%'", col, bh));
            var tmpBh = DataUtils.Instance.Dao.ExecuteScare(info).ToString();
            if (string.IsNullOrEmpty(tmpBh))
            {
                bh += "001";
            }
            else
            {
                string a = tmpBh.Substring(15, 3);
                bh += (int.Parse(a) + 1).ToString("000");
                //bh =( int64.Parse(tmpBh.ToString()) + 1).ToString();
            }
            return bh;
         //   return DateTime.Now.Ticks.ToString();
        }
        public void SaveImage(System.Drawing.Image img, string rybh)
        {
            System.IO.MemoryStream Ms = new MemoryStream();
            img.Save(Ms, System.Drawing.Imaging.ImageFormat.Jpeg);
            byte[] by = new byte[Ms.Length];
            Ms.Position = 0;
            Ms.Read(by, 0, Convert.ToInt32(Ms.Length));
            Ms.Close();
            Framework.QueryInfo info = new QueryInfo();
            info.QueryObject = "t_ry_xp";
            info.AddParam("RYDH", rybh);
            var o = DataUtils.Instance.Dao.FindOne(info) as Contract.Domain.t_ry_xp;

            if (o != null)
            {
                MySqlCommand cmd = new MySqlCommand();
                cmd.CommandText = "update t_ry_xp set xp=?XP where XPID='" + o.Id + "'";
                cmd.Parameters.Add(new MySqlParameter("?XP", MySqlDbType.LongBlob, by.Length)).Value = by;
                DataUtils.Instance.ExcutExecuteNonQuery(cmd);
            }
            else
            {
                MySqlCommand cmd = new MySqlCommand();
                cmd.CommandText = "insert into t_ry_xp(RYDH,XP)values(?RYDH,?XP)";
                cmd.Parameters.Add("?RYDH", MySqlDbType.VarString).Value = rybh;
                cmd.Parameters.Add(new MySqlParameter("?XP", MySqlDbType.LongBlob, by.Length)).Value = by;
                DataUtils.Instance.ExcutExecuteNonQuery(cmd);
            }
        }


        /// <summary>
        /// 将IList转为DataTable
        /// </summary>
        /// <param name="list"></param>
        /// <returns></returns>
        public DataTable ToDataTable(IList list)
        {
            DataTable result = new DataTable();
            if (list.Count > 0)
            {
                PropertyInfo[] propertys = list[0].GetType().GetProperties();
                foreach (PropertyInfo pi in propertys)
                {
                    //全用string 代替，带问号的会出问题
                    result.Columns.Add(pi.Name, typeof(string));
                }

                for (int i = 0; i < list.Count; i++)
                {
                    ArrayList tempList = new ArrayList();
                    foreach (PropertyInfo pi in propertys)
                    {
                        object obj = pi.GetValue(list[i], null);
                        tempList.Add(obj);
                    }
                    object[] array = tempList.ToArray();
                    result.LoadDataRow(array, true);
                }
            }

            return result;
        }

        public void test()
        {
            GetDcpFromEnums(typeof(EMenus));
        }
    }

    #region enums
    public enum EMenus
    {
        人员档案管理 = 4100,
        特情人员管理,
        特情人员审核,
        特情工作布置,
        特情保护协调,
        特情奖励,
        重点刑嫌人员管理,
        重点刑嫌人员审核,
        重点刑嫌人员交控登记,
        重点刑嫌人员调控登记,
        侦查阵地登记,
        侦查阵地登记审核,

        业务应用 = 4200,
        特情线索档案,
        特情线索档案审核,
        打击处理刑嫌,
        线索处理结果,

        综合查询 = 4300,
        特情人员查询,
        重点刑嫌人员查询,
        阵控点查询,
        线索关联查询,
        数据导入导出查询,

        考核统计 = 4400,
        刑事特情数据统计,
        特情发挥情况统计,
        未提供线索情况统计,
        线索使用情况统计,
        线索交叉发挥情况统计案件,
        线索交叉发挥情况统计人员,
        阵控点发挥情况统计,
        全省刑侦基础工作统计分析,
        月破案情况登记,

        用户管理主 = 4500,
        角色管理,
        用户管理,

        系统管理 = 4600,
        单位管理,
        民警信息维护,
        //数据导出,
        数据导出派出所,
        数据导出分局,
        数据导出市局,
        数据导入,
        表码管理,
        全局参数设置
    }

    public enum EOperateType
    {
        Add = 0,
        Delete,
        Modify, //修改
        Change, //变更
        Detail,
        Verify
    }
    /// <summary>
    /// 角色类型
    /// </summary>
    public enum RoleType
    {
        超级管理员 = 0,
        市局市局一般用户,
        分局一般用户,
        派出所一般用户
    }
    #endregion
}
